#include "output_file.h"
#include "../../config/app_config.h"
#include "../../common/functions.h"
#include "../../CoorTransform/CoorTransform.h"
#include <atomic>

#define OUTPUT_DATA_MAX_LEN (2048)
#define C (299792458.0)

OutputerFile::OutputerFile()
{
    m_is_output_ = false;

    m_type = OUTPUT_FILE_TYPE;
}

OutputerFile::~OutputerFile()
{
    if (m_log_file.get() != nullptr && m_log_file->is_running())
    {
        m_log_file->stop();
    }
}

void OutputerFile::StartOutput()
{
    BaseOutputer::StartOutput();

    m_log_file.reset(new async_logging());

    m_log_file->set_is_rool(false);
    m_log_file->set_is_specify_name(true);
    m_log_file->set_suffixname(".csv");
    m_log_file->set_base_name(generate_file_name());

    m_log_file->start();
}

void OutputerFile::StopOutPut()
{
    BaseOutputer::StopOutPut();

    if (m_log_file)
        m_log_file->stop();
}

std::string OutputerFile::generate_file_name()
{
    time_t the_cur_time = time(NULL);
    struct tm * cur_time = localtime(&the_cur_time);

    char file_name[MAX_PATH];
    int base_name_len = sprintf_s(file_name, MAX_PATH, "datalog\\%04d%02d%02d_%02d%02d%02d",
        cur_time->tm_year + 1900, cur_time->tm_mon + 1, cur_time->tm_mday,
        cur_time->tm_hour, cur_time->tm_min, cur_time->tm_sec);

    std::string log_base_name = std::string(file_name, base_name_len);

    return log_base_name;
}

void OutputerFile::UpdateRadarPoint(CCVBlockInfo *message)
{
    if (!m_is_output_)
    {
        return;
    }

    char output_data[OUTPUT_DATA_MAX_LEN];

    std::string m_time = CommonFunction::time2String(time(NULL));

    double longtitude, latitude, altitude;

    CoorTransformNoCenter::RAE2JWH(sln_app_config::instance().get_radar_longitude(), sln_app_config::instance().get_radar_latitude(),
        sln_app_config::instance().get_radar_altitude(), message->Range, message->Azimuth, 0, longtitude, latitude, altitude);

    double time_delta = 2 * message->Range / C;
    double t_strong = message->WSum / (double)message->SampleSize;

    if (t_strong > 255)
    {
        t_strong = 255;
    }

    int data_str_len = sprintf_s(output_data, OUTPUT_DATA_MAX_LEN, "%d,%s,%d,%d,%.2f,%.9f,%.6f,%.6f,%.3f,%.3f,%.3f,%.3f\n",
        0,
        m_time.c_str(),
        0,
        0,
        t_strong,
        time_delta,
        longtitude,
        latitude,
        message->Range,
        message->Azimuth,
        0.0,
        0.0
        );

    m_log_file->append(output_data, data_str_len);
}

void OutputerFile::UpdateRadarInfo(const radar_info* info)
{
    if (!m_is_output_)
    {
        return;
    }

    char output_data[OUTPUT_DATA_MAX_LEN];

    std::string m_time = CommonFunction::time2String(info->timestamp);

    double time_delta = 2 * info->r / C;

    int data_str_len = sprintf_s(output_data, OUTPUT_DATA_MAX_LEN, "%d,%s,%d,%d,%.2f,%.9f,%.6f,%.6f,%.3f,%.3f,%.3f,%.3f\n",
        info->radar_id,
        m_time.c_str(),
        info->tv_msec,
        0,
        0.0,
        time_delta,
        info->longitude,
        info->latitude,
        info->r,
        info->a,
        info->speed,
        info->heading
        );

    m_log_file->append(output_data, data_str_len);
}